package com.eastfair.home.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.eastfair.annotation.annotation.log.SysLog;
import com.eastfair.annotation.annotation.security.PreAuth;
import com.eastfair.boot.controller.SuperController;
import com.eastfair.boot.request.PageParams;
import com.eastfair.core.base.R;
import com.eastfair.core.exception.BizException;
import com.eastfair.echo.core.EchoService;
import com.eastfair.home.dto.VenueReportDTO;
import com.eastfair.home.dto.VenueReportPageQuery;
import com.eastfair.home.dto.VenueReportSaveDTO;
import com.eastfair.home.dto.VenueReportUpdateDTO;
import com.eastfair.home.entity.VenueReport;
import com.eastfair.home.service.VenueReportService;
import com.eastfair.home.vo.VenueReportVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * <p>
 * 前端控制器
 * 一键报馆记录
 * </p>
 *
 * @author dq
 * @date 2022-10-31
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/venueReport")
@Api(value = "VenueReport", tags = "一键报馆记录")
@PreAuth(replace = "home:venueReport:")
public class VenueReportController extends SuperController<VenueReportService, Long, VenueReport, VenueReportPageQuery, VenueReportSaveDTO, VenueReportUpdateDTO> {

    @Resource
    private EchoService echoService;

    @Resource
    private VenueReportService venueReportService;

    @Override
    public void handlerResult(IPage<VenueReport> page) {
        // 想让返回值实现自动回显，请将此行代码打开
        // echoService.action(page);
    }

    /**
     * Excel导入后的操作
     *
     * @param list
     */
    @Override
    public R<Boolean> handlerImport(List<Map<String, String>> list) {
        List<VenueReport> venueReportList = list
                .stream()
                .map((map) -> VenueReport.builder().build())
                .collect(Collectors.toList());
        return R.success(baseService.saveBatch(venueReportList));
    }

    /**
     * 处理参数
     *
     * @param params 分页参数
     */
    @Override
    public void handlerQueryParams(PageParams<VenueReportPageQuery> params) {
        VenueReportPageQuery pageQuery = params.getModel();
    }

    /**
     * 查询主场报馆列表VO.
     *
     * @param query the query
     * @return the r
     */
    @ApiOperation("查询主场报馆列表VO")
    @PostMapping("/query/venueReport")
    public R<List<VenueReportVO>> queryVenueReport(@RequestBody VenueReportDTO query) {
        log.info("queryVenueReport - 查询主场报馆列表VO, query={}", query);
        return R.successDef(venueReportService.listVenueReportVO(query));
    }

    /**
     * 批量保存主场报馆记录.
     *
     * @param list the list
     * @return the r
     */
    @ApiOperation("批量保存主场报馆记录")
    @SysLog("添加报馆记录")
    @PostMapping("/saveBatch")
    public R<Boolean> saveBatch(@RequestBody List<VenueReportDTO> list) {
        log.info("saveBatch - 批量保存主场报馆记录, list={}", list);
        try {
            return R.successDef(venueReportService.saveBatch(list));
        } catch (BizException e) {
            return R.fail(e);
        }
    }
}
